<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-select
          v-model="queryParams.supplierId"
          clearable
          placeholder="请选择供应商"
        >
          <el-option
            :label="supplier.supName"
            :value="supplier.id"
            v-for="supplier in supplierList"
            :key="supplier.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select
          v-model="queryParams.customerId"
          clearable
          placeholder="请选择客户"
        >
          <el-option
            :label="customer.cusName"
            :value="customer.id"
            v-for="customer in customerList"
            :key="customer.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据阶段" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          clearable
          placeholder="请选择"
        >
          <el-option
            :label="orderStatus.name"
            :value="orderStatus.id"
            v-for="orderStatus in orderStatusList"
            :key="orderStatus.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据类型" prop="paymentType">
        <el-select
          v-model="queryParams.paymentType"
          clearable
          placeholder="请选择"
        >
          <el-option
            :label="paymentType.name"
            :value="paymentType.id"
            v-for="paymentType in paymentTypeList"
            :key="paymentType.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-date-picker
          v-model="daterangeOrderDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="生效日期">
        <el-date-picker
          v-model="daterangeEffectDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间">
        <el-date-picker
          v-model="daterangeModifyDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格上方按钮 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:reciveReq:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:reciveReq:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 中部数据表格 -->
    <el-table
      v-loading="loading"
      :data="reciveReqList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="70" align="left" fixed="left">
        <template slot-scope="scope">
          {{
            scope.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="单据编号"
        align="center"
        prop="orderCode"
        fixed="left"
        width="120"
      >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            :content="scope.row.orderCode"
          >
            <div slot="reference" class="ellipsis-text">
              <el-link type="primary" @click="showReciveReq(scope.row)">{{
                scope.row.orderCode
              }}</el-link>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="单据日期"
        align="center"
        prop="orderDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据阶段" align="center" prop="orderStatusName">
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="paymentTypeName">
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customer.cusName" />
      <el-table-column label="供货商" align="center" prop="supplies.supName" />
      <el-table-column
        label="业务员"
        align="center"
        prop="orderUser.userName"
      />
      <el-table-column
        label="源单号"
        align="center"
        prop="srcOrderCode"
        width="120"
      >
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="hover"
            :content="scope.row.srcOrderCode"
          >
            <div slot="reference" class="ellipsis-text">
              {{ scope.row.srcOrderCode }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="money" width="120" />
      <el-table-column
        label="已核销金额"
        align="center"
        prop="verMoney"
        width="120"
      />
      <el-table-column
        label="生效日期"
        align="center"
        prop="effectDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人"
        align="center"
        prop="approvalUser.userName"
      />
      <el-table-column
        label="审核结果"
        align="center"
        prop="approvalResultName"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.approvalResult == 2">{{
            scope.row.approvalResultName
          }}</el-tag>
          <el-tag type="danger" v-if="scope.row.approvalResult == 3">{{
            scope.row.approvalResultName
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="approvalOption" />
      <el-table-column label="是否生效" align="center" prop="isEffectName">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            :content="scope.row.remark"
          >
            <div slot="reference" class="ellipsis-text">
              {{ scope.row.remark }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifyBy" />
      <el-table-column
        label="修改时间"
        align="center"
        prop="modifyDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:reciveReq:edit']"
            :disabled="scope.row.orderStatus != 2"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:reciveReq:remove']"
            :disabled="scope.row.approvalResult != 3"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应收需求单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :disabled="formDisabled"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据编号" prop="orderCode">
              <el-input
                v-model="form.orderCode"
                placeholder="请输入"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据日期" prop="orderDate">
              <el-date-picker
                clearable
                v-model="form.orderDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择单据日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerId">
              <el-select
                v-model="form.customerId"
                clearable
                placeholder="请选择客户"
              >
                <el-option
                  :label="customer.cusName"
                  :value="customer.id"
                  v-for="customer in customerList"
                  :key="customer.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="金额" prop="money">
              <el-input-number
                :min="0"
                :max="999999999"
                :precision="2"
                :disabled="formDisabled"
                v-model.number="form.money"
                placeholder="请输入金额"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="业务员" prop="orderUser.userName">
              <el-input
                :value="form.orderUser.userName"
                placeholder="请选择业务员"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-button @click="handleSelectUser">选择业务员</el-button>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务部门" prop="orderDeptName">
              <el-input
                :value="form.orderDeptName"
                placeholder="请输入"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <i
          class="el-icon-arrow-down"
          @click="showFormDetail"
          style="cursor: pointer"
        ></i>
        <el-divider></el-divider>
        <div v-show="formDetailDisabled">
          <el-row>
            <el-col :span="8">
              <el-form-item label="单据阶段" prop="orderStatusName">
                <el-input
                  :value="form.orderStatusName"
                  placeholder=""
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="单据类型" prop="paymentTypeName">
                <el-input
                  :value="form.paymentTypeName"
                  placeholder=""
                  disabled
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="供应商" prop="supplierId">
                <el-select v-model="form.supplierId" disabled placeholder="">
                  <el-option
                    :label="supplier.supName"
                    :value="supplier.id"
                    v-for="supplier in supplierList"
                    :key="supplier.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="源单号" prop="srcOrderCode">
                <el-input :value="form.srcOrderCode" placeholder="" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="已核销金额" prop="verMoney">
                <el-input :value="form.verMoney" placeholder="" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生效日期" prop="effectDate">
                <el-date-picker
                  clearable
                  :value="form.effectDate"
                  type="date"
                  disabled
                  value-format="yyyy-MM-dd"
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="审核人" prop="approvalUser.userName">
                <el-input
                  :value="form.approvalUser.userName"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核结果" prop="approvalResultName">
                <el-input
                  :value="form.approvalResultName"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审核意见" prop="approvalOption">
                <el-input
                  type="textarea"
                  :value="form.approvalOption"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="创建人" prop="createBy">
                <el-input :value="form.createBy" placeholder="" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="修改人" prop="modifyBy">
                <el-input :value="form.modifyBy" placeholder="" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="创建时间" prop="createDate">
                <el-date-picker
                  clearable
                  :value="form.createDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="修改时间" prop="modifyDate">
                <el-date-picker
                  clearable
                  :value="form.modifyDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  disabled
                  placeholder=""
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="remark">
                <el-input
                  type="textarea"
                  v-model="form.remark"
                  placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-dialog
          width="60%"
          title="选择业务员"
          :visible.sync="innerUserVisible"
          append-to-body
        >
          <div class="app-container">
            <el-row :gutter="20">
              <!--部门数据-->
              <el-col :span="4" :xs="24">
                <div class="head-container">
                  <el-input
                    v-model="deptName"
                    placeholder="请输入部门名称"
                    clearable
                    size="small"
                    prefix-icon="el-icon-search"
                    style="margin-bottom: 20px"
                  />
                </div>
                <div class="head-container">
                  <el-tree
                    :data="deptOptions"
                    :props="defaultProps"
                    :expand-on-click-node="false"
                    :filter-node-method="filterNode"
                    ref="tree"
                    node-key="id"
                    default-expand-all
                    highlight-current
                    @node-click="handleNodeClick"
                  />
                </div>
              </el-col>
              <!--用户数据-->
              <el-col :span="20" :xs="24">
                <el-form
                  :model="queryUserParams"
                  ref="queryUserForm"
                  size="small"
                  :inline="true"
                  v-show="showUserSearch"
                  label-width="68px"
                >
                  <el-form-item label="用户名称" prop="userName">
                    <el-input
                      v-model="queryUserParams.userName"
                      placeholder="请输入用户名称"
                      clearable
                      style="width: 240px"
                      @keyup.enter.native="handleUserQuery"
                    />
                  </el-form-item>
                  <el-form-item>
                    <el-button
                      type="primary"
                      icon="el-icon-search"
                      size="mini"
                      @click="handleUserQuery"
                      >搜索</el-button
                    >
                    <el-button
                      icon="el-icon-refresh"
                      size="mini"
                      @click="resetUserQuery"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-form>
                <el-table
                  v-loading="userLoading"
                  :data="userList"
                  highlight-current-row
                  @current-change="handleCurrentChange"
                >
                  <el-table-column
                    label="用户编号"
                    align="center"
                    key="userId"
                    prop="userId"
                    v-if="columns[0].visible"
                  />
                  <el-table-column
                    label="用户名称"
                    align="center"
                    key="userName"
                    prop="userName"
                    v-if="columns[1].visible"
                    :show-overflow-tooltip="true"
                  />
                  <el-table-column
                    label="用户昵称"
                    align="center"
                    key="nickName"
                    prop="nickName"
                    v-if="columns[2].visible"
                    :show-overflow-tooltip="true"
                  />
                  <el-table-column
                    label="部门"
                    align="center"
                    key="deptName"
                    prop="dept.deptName"
                    v-if="columns[3].visible"
                    :show-overflow-tooltip="true"
                  />
                  <el-table-column
                    label="手机号码"
                    align="center"
                    key="phonenumber"
                    prop="phonenumber"
                    v-if="columns[4].visible"
                    width="120"
                  />
                  <el-table-column
                    label="创建时间"
                    align="center"
                    prop="createTime"
                    v-if="columns[6].visible"
                    width="160"
                  >
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                  </el-table-column>
                </el-table>

                <pagination
                  v-show="userTotal > 0"
                  :total="userTotal"
                  :page.sync="queryUserParams.pageNum"
                  :limit.sync="queryUserParams.pageSize"
                  @pagination="getUserList"
                />
              </el-col>
            </el-row>
          </div>
        </el-dialog>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formDisabled"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReciveReq,
  getReciveReqById,
  delReciveReq,
  addReciveReq,
  updateReciveReq,
} from "@/api/finance/reciveReq";
import { listUser, deptTreeSelect } from "@/api/system/user";
import { getSupplies, listAllSupplies } from "@/api/basedata/supplies";
import { getAllCus } from "@/api/basedata/customer";

export default {
  name: "ReciveReq",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //显示用户搜索条件
      showUserSearch: true,
      // 总条数
      total: 0,
      // 应收需求单表格数据
      reciveReqList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeOrderDate: [],
      // 备注时间范围
      daterangeEffectDate: [],
      // 备注时间范围
      daterangeCreateDate: [],
      // 备注时间范围
      daterangeModifyDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        orderDate: null,
        orderStatus: null,
        paymentType: null,
        customerId: null,
        supplierId: null,
        srcOrderCode: null,
        effectDate: null,
        approvalResult: null,
        createDate: null,
        modifyDate: null,
      },
      // 单据阶段列表
      orderStatusList: [
        {
          id: 2,
          name: "已提交",
        },
        {
          id: 3,
          name: "已审批",
        },
        {
          id: 4,
          name: "执行完",
        },
      ],
      // 单据类型列表
      paymentTypeList: [
        {
          id: 101,
          name: "销售应收",
        },
        {
          id: 102,
          name: "采购退货应收",
        },
        {
          id: 103,
          name: "其他应收",
        },
      ],
      // 审批结果列表
      approvalList: [
        {
          id: 0,
          name: "",
        },
        {
          id: 1,
          name: "未审批",
        },
        {
          id: 2,
          name: "已通过",
        },
        {
          id: 3,
          name: "已驳回",
        },
      ],
      //是否生效
      isEffectList: [
        {
          id: 0,
          name: "否",
        },
        {
          id: 1,
          name: "是",
        },
      ],
      // 供应商列表
      supplierList: [],
      // 客户列表
      customerList: [],
      // 表单参数
      form: {
        orderUser: { userName: "" },
        approvalUser: { userName: "" },
      },
      //财务应收单详细内容
      formDetailDisabled: false,
      // 表单禁用
      formDisabled: false,
      /**  用户内部对话框 */
      userLoading: false,
      deptName: "",
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      //内部用户对话框
      innerUserVisible: false,
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true },
      ],
      // 用户查询参数
      queryUserParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
      //用户时间范围
      dateRange: [],
      // 用户表格数据
      userList: null,
      // 用户总条数
      userTotal: 0,

      // 表单校验
      rules: {
        orderCode: [
          { required: true, message: "单据编号不能为空", trigger: "blur" },
        ],
        orderDate: [
          { required: true, message: "单据日期不能为空", trigger: "blur" },
        ],
        customerId: [
          { required: true, message: "客户不能为空", trigger: "change" },
        ],
        orderStatus: [
          { required: true, message: "单据阶段不能为空", trigger: "change" },
        ],
        money: [{ required: true, message: "金额不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getSuppliesList();
    this.getCustomerList();
  },
  methods: {
    /** 获取供应商列表 */
    getSuppliesList() {
      listAllSupplies().then((response) => {
        this.supplierList = response.data;
      });
    },
    /** 获取客户列表 */
    getCustomerList() {
      getAllCus().then((response) => {
        this.customerList = response.data;
      });
    },
    /** 查询应收需求单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderDate && "" != this.daterangeOrderDate) {
        this.queryParams.params["beginOrderDate"] = this.daterangeOrderDate[0];
        this.queryParams.params["endOrderDate"] = this.daterangeOrderDate[1];
      }
      if (null != this.daterangeEffectDate && "" != this.daterangeEffectDate) {
        this.queryParams.params["beginEffectDate"] =
          this.daterangeEffectDate[0];
        this.queryParams.params["endEffectDate"] = this.daterangeEffectDate[1];
      }
      if (null != this.daterangeCreateDate && "" != this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] =
          this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      if (null != this.daterangeModifyDate && "" != this.daterangeModifyDate) {
        this.queryParams.params["beginModifyDate"] =
          this.daterangeModifyDate[0];
        this.queryParams.params["endModifyDate"] = this.daterangeModifyDate[1];
      }
      listReciveReq(this.queryParams).then((response) => {
        this.reciveReqList = response.rows;
        console.log(this.reciveReqList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        orderDate: null,
        orderStatus: null,
        paymentType: null,
        customerId: null,
        supplierId: null,
        orderUserId: null,
        srcOrderCode: null,
        orderUserId: null,
        orderUserName: null,
        orderDeptName: null,
        orderDeptId: null,
        money: null,
        verMoney: null,
        effectDate: null,
        approvalId: null,
        approvalResult: null,
        approvalOption: null,
        isEffect: null,
        isClose: null,
        isCancel: null,
        remark: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null,
        orderUser: { userName: "" },
        approvalUser: { userName: "" },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.daterangeEffectDate = [];
      this.daterangeCreateDate = [];
      this.daterangeModifyDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.orderCode = "CWYS" + Date.now();
      this.open = true;
      this.formDisabled = false;
      this.title = "添加其他应收单";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getReciveReq(row);
      this.open = true;
      this.formDisabled = false;
      this.title = "修改其他应收单";
    },
    //展示财务应收单详情
    showReciveReq(row) {
      this.reset();
      this.getReciveReq(row);
      this.open = true;
      this.formDisabled = true;
      this.title = "查看财务应收单";
    },
    //获取财务应收单
    getReciveReq(row) {
      const id = row.id || this.ids;
      getReciveReqById(id).then((response) => {
        this.form = response.data;
        if (this.form.approvalUser == null) {
          this.form.approvalUser = { userName: "" };
        }
        if (this.form.orderUser == null) {
          this.form.orderUser = { userName: "" };
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateReciveReq(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.paymentType = 103;
            addReciveReq(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除应收需求单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delReciveReq(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "finance/reciveReq/export",
        {
          ...this.queryParams,
        },
        `reciveReq_${new Date().getTime()}.xlsx`
      );
    },

    /**
     *
     * @param val 内部用户对话框相关
     */
    /** 搜索按钮操作 */
    handleUserQuery() {
      this.queryParams.pageNum = 1;
      this.getUserList();
    },
    /** 重置业务员按钮操作 */
    resetUserQuery() {
      this.resetForm("queryUserForm");
      this.queryUserParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleUserQuery();
    },
    /** 查询用户列表 */
    getUserList() {
      this.userLoading = true;
      listUser(this.addDateRange(this.queryUserParams, this.dateRange)).then(
        (response) => {
          this.userList = response.rows;
          this.userTotal = response.total;
          this.userLoading = false;
        }
      );
    },
    //用户单选框选中数据
    handleCurrentChange(val) {
      this.currentRow = val;
      console.log(val);
      this.form.orderUserId = val.userId;
      this.form.orderUser.userName = val.userName;
      this.form.orderDeptName = val.dept.deptName;
      this.form.orderDeptId = val.deptId;
      this.innerUserVisible = false;
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    /** 重置按钮操作 */
    resetUserQuery() {
      this.resetForm("queryUserForm");
      this.queryUserParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleUserQuery();
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryUserParams.deptId = data.id;
      this.handleUserQuery();
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 选择用户按钮操作 */
    handleSelectUser() {
      this.innerUserVisible = true;
      this.getUserList();
      this.getDeptTree();
    },
    // 显示表单详细信息
    showFormDetail() {
      this.formDetailDisabled = !this.formDetailDisabled;
    },
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
};
</script>

<style scoped>
/**  长度过长的文本省略号 */
.ellipsis-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100px;
}
</style>
